package com.wzl.mixed.controller;

import java.util.ArrayList;
import java.util.List;

/**
 * @author weizhongli
 * @since 1.0, 2023/02/13 10:54
 */
public class tastMain {

    public static void main(String[] args) {

//        List<String> billingCycles = Arrays.asList();
        List<String> billingCycles = new ArrayList<>();
        billingCycles.add("12312");
        billingCycles.add("12401");
        billingCycles.add("12402");
        billingCycles.add("12311");
        billingCycles.add("12405");
        String result = checkBillingCycleContinuity(billingCycles);
        System.out.println(result);


    }

    public static String checkBillingCycleContinuity(List<String> billingCycles) {

//        String[] billingCycleArr = billingCycles.split(",");
//        // 新的 String 数组，与原始数组长度相同
//        String[] processedArray = new String[billingCycleArr.length];
//        for (int i = 0; i < billingCycleArr.length; i++) {
//            processedArray[i] = String.valueOf(Long.parseLong(billingCycleArr[i]) + 190000L);
//        }
//        Arrays.sort(processedArray);

        List<String> arrayList = new ArrayList();
        for (String billingCycle : billingCycles) {
            billingCycle = String.valueOf(Long.parseLong(billingCycle) + 190000L);
            arrayList.add(billingCycle);
        }

        // 对账期进行排序
        arrayList.sort(String::compareTo);


        StringBuilder result = new StringBuilder("");
        StringBuilder startBillingCycle = new StringBuilder("");
        StringBuilder endBillingCycle = new StringBuilder("");

        for (String billingCycle : arrayList) {
            StringBuilder stringBuilder = new StringBuilder(billingCycle);

            if (startBillingCycle.toString().isEmpty()) {
                startBillingCycle = stringBuilder;
                endBillingCycle = stringBuilder;
            } else {
                int currentCycle = Integer.parseInt(billingCycle);
                int previousCycle = Integer.parseInt(String.valueOf(endBillingCycle));
                if (currentCycle == previousCycle + 1) {
                    endBillingCycle = stringBuilder;
                } else if(currentCycle == previousCycle + 89) {
                    endBillingCycle = stringBuilder;
                }else {
                    if (!startBillingCycle.equals(endBillingCycle)) {
                        result.append(startBillingCycle).append("-").append(endBillingCycle).append(",");
                    } else {
                        result.append(startBillingCycle).append(",");
                    }
                    startBillingCycle = stringBuilder;
                    endBillingCycle = stringBuilder;

                }
            }
        }

        // 处理最后一个连续的账期范围
        if (!startBillingCycle.toString().isEmpty()) {
            if (!startBillingCycle.equals(endBillingCycle)) {
                result.append(startBillingCycle).append("-").append(endBillingCycle);
            } else {
                result.append(startBillingCycle);
            }
        }

        return result.toString();
    }

}
